package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.ArticleDto;
import com.itheima.ydd.entity.Article;
import com.itheima.ydd.entity.Cate;
import com.itheima.ydd.service.ArticleService;
import com.itheima.ydd.service.CateService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

//文章管理
@RestController
@RequestMapping("/ydd_article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @Autowired
    private CateService cateService;

    //文章管理-分页查询
    @GetMapping("/list")
    public ResultData list(Long start_time, Long end_time,
                           String title, Long cateid, int page , int limit){

        Page<Article> articlePage = new Page<>(page, limit);
        Page<ArticleDto> articleDtoPage = new Page<>();
        //构造条件查询
        LambdaQueryWrapper<Article> lqw = new LambdaQueryWrapper<>();
        lqw.in(start_time != null && end_time != null,Article::getCreate_date,start_time,end_time);
        lqw.like(StringUtils.isNotEmpty(title),Article::getTitle,title);
        lqw.eq(cateid != null,Article::getCateid,cateid);
        lqw.orderByDesc(Article::getCreate_date);
        //查询
        articleService.page(articlePage,lqw);
        //对象复制
        BeanUtils.copyProperties(articlePage,articleDtoPage,"records");
        List<Article> records = articlePage.getRecords();

        List<ArticleDto> articleDtos = records.stream().map((item)->{
            ArticleDto articleDto = new ArticleDto();
            BeanUtils.copyProperties(item,articleDto);

            Integer cateid2 = item.getCateid();

            Cate cate = cateService.getById(cateid2);
            if(cate != null){
                String catename = cate.getCatename();
                articleDto.setCatename(catename);
            }

            return articleDto;
        }).collect(Collectors.toList());

        articleDtoPage.setRecords(articleDtos);
        return ResultData.ok("", PageResult.init(articleDtoPage.getTotal(),
                articleDtoPage.getSize(),
                articleDtoPage.getCurrent(),
                articleDtoPage.getRecords()));
    }

    //文章管理-删除文章
    @GetMapping("/del")
    public ResultData delete(Long id){

        articleService.removeById(id);
        return ResultData.ok("","文章删除成功！");
    }

    //文章管理-根据Id查询数据获取详情
    @GetMapping("/rowInfo")
    public ResultData rowInfo(Long id){

        Article article = articleService.getById(id);
        ArticleDto articleDto = new ArticleDto();
        BeanUtils.copyProperties(article,articleDto);

        //获取分类Id
        Integer cateid = article.getCateid();

        Cate cate = cateService.getById(cateid);
        if(cate != null){
            articleDto.setCatename(cate.getCatename());
        }
        return ResultData.ok("",articleDto);
    }

    @PostMapping("/add")
    public ResultData add(@RequestBody Article article){
        articleService.save(article);
        return ResultData.ok("","文章添加成功！");
    }
}
